
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0835
 * @Author : WangRuoyu 
 * @Date : 2023/9/18 14:44
 */

#include "iostream"

using namespace std;

const int N = 1e5 + 10;
int son[N][26];
int cnt[N];
int idx = 0;

void insert(string s) {
    int p = 0;
    for (int i = 0; i < s.size(); ++i) {
        int u = s[i] - 'a';
        if (!son[p][u]) son[p][u] = ++idx;
        p = son[p][u];
    }
    cnt[p]++;
}

int query(string s) {
    int p = 0;
    for (int i = 0; i < s.size(); ++i) {
        int u = s[i] - 'a';
        if (!son[p][u]) return 0;
        p = son[p][u];
    }
    return cnt[p];
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        char op;
        string s;
        cin >> op >> s;
        if (op == 'I') {
            insert(s);
        } else {
            printf("%d\n", query(s));
        }
    }
    return 0;
}